package com.ant.exceptions;

import com.ant.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.resource.NoResourceFoundException;

import java.util.Map;

/**
 * @Auther: 灏
 * @Date: 2025/7/13 - 07 - 13 - 04:39
 * @Description: com.ant.exceptions
 * @version: 1.0
 */
@Slf4j
@ControllerAdvice
@ResponseBody
public class ExceptionHandler extends GlobalExceptionHandler{
    @org.springframework.web.bind.annotation.ExceptionHandler(NoResourceFoundException.class)
    public Result<String> noResourceFoundException(NoResourceFoundException ex, WebRequest request) {
        String method = request.getHeader("X-HTTP-Method-Override");
        if (method == null) {
            method = request.getHeader("X-HTTP-Method");
        }
        String host = request.getHeader("Host");
        String shortDescription = request.getDescription(false);
        Result<String> resultFailed = Result.failed(HttpStatus.NOT_FOUND.value(), ex.toString(),method+" "+host+shortDescription +"资源未找到");
        log.error(ex.toString());
        ex.printStackTrace();
        return resultFailed;
    }
}
